import { Leafer, PointerEvent, DragEvent } from "leafer-ui";
import type { IToolConfig } from "../types";

/**
 * 工具基类（抽象类）
 * 所有具体工具需继承此类
 */
export abstract class BaseTool {
  // 工具类型标识
  abstract readonly name: string;

  // 需要监听的事件类型（由子类指定）
  protected abstract events: string[];

  constructor(
    protected readonly app: Leafer,
    protected config: IToolConfig
  ) {}

  // 激活工具时的初始化操作
  activate() {
    this.registerEvents();
  }

  // 停用工具时的清理操作
  deactivate() {
    this.unregisterEvents();
  }

  // 事件处理映射（子类实现具体逻辑）
  protected abstract onPointerDown(e: PointerEvent): void;
  protected abstract onPointerMove(e: PointerEvent): void;
  protected abstract onPointerUp(e: PointerEvent): void;
  protected abstract onDragStart(e: DragEvent): void;
  protected abstract onDragMove(e: DragEvent): void;
  protected abstract onDragEnd(e: DragEvent): void;

  // 自动事件注册（根据events数组）
  private registerEvents() {
    this.events.forEach((event) => {
      console.log(event)
      switch (event) {
        case PointerEvent.DOWN:
          this.app.on(PointerEvent.DOWN, this.onPointerDown);
          break;
        case DragEvent.START:
          this.app.on(DragEvent.START, this.onDragStart);
          break;
        // 其他事件同理...
      }
    });
  }

  private unregisterEvents() {
    this.events.forEach((event) => {
      switch (event) {
        case PointerEvent.DOWN:
          this.app.off(PointerEvent.DOWN, this.onPointerDown);
          break;
        case DragEvent.START:
          this.app.off(DragEvent.START, this.onDragStart);
          break;
        // 其他事件同理...
      }
    });
  }
}
